//中大团队拓展
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var mytime=new Date();

Page({
  data: {
    orderList: [],
    monthbegin:'',
    monthend:'',
    page: 1,
    size: 10,
    loadmoreText: '正在加载更多数据',
    nomoreText: '全部加载完成',
    nomore: false,
    totalPages: 1,
    year:0,
    month:0,
    totalPrice:0,
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面显示
    wx.showLoading({
      title: '加载中...',
      success: function () {
      }
    });
    this.getOrderList();


    this.setData({year: mytime.getFullYear(),
                 month: mytime.getMonth()+1})
    let that=this;
    //本月起始时间
    //本月结束时间
    var end='';
    //当前月份
    let mon=that.data.month;
    //当前月份的两位数形式
    let monInTwo=''
    if(mon<10)
    monInTwo='0'+mon;
    else
    monInTwo=''+mon;

    var begin = that.data.year + '-' + monInTwo + '-' + "01" + " 00:00:00";
    //判断月底时间
    if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon ==12)
      end = that.data.year + '-' + monInTwo +'-' + 31 + " 23:59:59";
    else if (that.data.month == 4 || mon == 6 || mon == 9 || mon ==11)
      end = that.data.year + '-' + monInTwo + '-' + 30 + " 23:59:59";
    else if (that.data.year % 4 == 0 && that.data.year % 100 != 0)
      end = that.data.year + '-' + monInTwo + '-' + 29 + " 23:59:59";
    else 
      end = that.data.year + '-' + monInTwo + '-' + 28 + " 23:59:59";
    this.setData({
      monthbegin: begin,
      monthend: end
    })
    
  },

  /**
       * 页面上拉触底事件的处理函数
       */
  onReachBottom: function () {
    this.getOrderList()
  },

  getOrderList() {
    let that = this;

    if (that.data.totalPages <= that.data.page - 1) {
      that.setData({
        nomore: true
      })
      return;
    }

    util.request(api.OrderList, { page: that.data.page, size: that.data.size }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.data),
          page: res.data.currentPage + 1,
          totalPages: res.data.totalPages
        });
        wx.hideLoading();
      }
    });
  },
  payOrder(event) {
    let that = this;
    let orderIndex = event.currentTarget.dataset.orderIndex;
    let order = that.data.orderList[orderIndex];
    wx.redirectTo({
      url: '/pages/pay/pay?orderId=' + order.id + '&actualPrice=' + order.actual_price,
    })
  },
  onReady: function () {
    // 页面渲染完成
    //计算月结
    let that = this;
    let i = 0;
    let total = 0;
    for (i = 0; i < that.data.orderList.length; i++) {
      if (that.data.orderList[i].add_time >= that.data.monthbegin &&                    that.data.orderList[i].add_time <= that.data.monthend){
        total = total + that.data.orderList[i].actual_price;
      }
    }
    this.setData({ totalPrice: total })
    
  },

  onShow: function () {
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})